//
//  ZJDynamicTableViewCell.m
//  LovingPartyBuilding
//
//  Created by ZJ on 2017/6/6.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "ZJDynamicCommentTableViewCell.h"

@interface ZJDynamicCommentTableViewCell ()
/** 灰色背景 */
@property (nonatomic, strong) UIView *bgView;
/** 小箭头 */
@property (nonatomic, strong) UIImageView *arrows;
/** 下划线 */
@property (nonatomic, strong) UILabel *underLine;
/** 内容label */
@property (nonatomic, strong) UILabel *contentLabel;
@end

@implementation ZJDynamicCommentTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [self.contentView addSubview:self.bgView];
        [self.contentView addSubview:self.arrows];
        [self.contentView addSubview:self.underLine];
        [self.contentView addSubview:self.contentLabel];
        
        self.arrows.sd_layout
        .topSpaceToView(self.contentView, 0)
        .leftSpaceToView(self.contentView, 80 * [UIScreen zj_screenWScale])
        .widthIs(50 * [UIScreen zj_screenWScale])
        .heightIs(10 * [UIScreen zj_screenHScale]);
        
        self.contentLabel.sd_layout
        .topSpaceToView(self.arrows, 5 *[UIScreen zj_screenHScale])
        .leftSpaceToView(self.contentView, (62 + 5) * [UIScreen zj_screenWScale])
        .widthIs([UIScreen zj_screenWith] - (62 + 12 + 10) * [UIScreen zj_screenWScale])
        .heightIs(20);
        
        self.bgView.sd_layout
        .topSpaceToView(self.arrows, 0)
        .leftSpaceToView(self.contentView, 62 * [UIScreen zj_screenWScale])
        .rightSpaceToView(self.contentView, 12 * [UIScreen zj_screenWScale])
        .heightIs(self.contentLabel.height + 10 * [UIScreen zj_screenHScale]);
        
        self.underLine.sd_layout
        .leftSpaceToView(self.contentView, 10 * [UIScreen zj_screenWScale])
        .rightSpaceToView(self.contentView, 10 * [UIScreen zj_screenWScale])
        .bottomSpaceToView(self.contentView, 0)
        .heightIs(1);

        
    }
    return self;
}



#pragma mark - Setter
- (void)setModel:(ZJCommentList *)model{
     NSMutableString *contentLabelText = [NSMutableString string];
    if (model.FFID.length > 0) {//被评论人存在
        [contentLabelText appendString:[NSString stringWithFormat:@"%@回复",model.UNAME]];
        [contentLabelText appendString:[NSString stringWithFormat:@"%@ : %@",model.FUNAME,model.CONTENT]];
        self.contentLabel.keywordsArray = @[model.UNAME,model.FUNAME];
        self.contentLabel.keywordsColor = [UIColor zj_colorWithHexString:@"646a8c"];
    }else{//没有评论人
        [contentLabelText appendString:[NSString stringWithFormat:@"%@ : %@",model.UNAME,model.CONTENT]];
        self.contentLabel.keywordsArray = @[model.UNAME];
        self.contentLabel.keywordsColor = [UIColor zj_colorWithHexString:@"646a8c"];
    }
    
   
    
    self.contentLabel.text = contentLabelText;
    
    if (self.indexPath.row == 0) {//是第一个(有箭头)
        self.arrows.hidden = NO;
        
        self.contentLabel.sd_layout
        .topSpaceToView(self.arrows, 5 *[UIScreen zj_screenHScale])
        .leftSpaceToView(self.contentView, (62 + 5) * [UIScreen zj_screenWScale])
        .widthIs([UIScreen zj_screenWith] - (62 + 12 + 10) * [UIScreen zj_screenWScale])
        .heightIs([self.contentLabel getLableSizeWithMaxWidth:self.contentLabel.width].height);
        [self.contentLabel updateLayout];
        
        self.bgView.sd_layout
        .topSpaceToView(self.arrows, 0)
        .leftSpaceToView(self.contentView, 62 * [UIScreen zj_screenWScale])
        .rightSpaceToView(self.contentView, 12 * [UIScreen zj_screenWScale])
        .heightIs(self.contentLabel.height + 10 * [UIScreen zj_screenHScale]);
        [self.bgView updateLayout];
        
        if (self.talkModel.COMMENTLIST.count == 1) {//只有一个
            self.underLine.hidden = NO;
            [self.bgView setRadius:4 * [UIScreen zj_screenWScale] byRoundingCorners:UIRectCornerAllCorners];
        }else{//是第一个但不是最后一个
            self.underLine.hidden = YES;
            [self.bgView setRadius:4 * [UIScreen zj_screenWScale] byRoundingCorners:UIRectCornerTopRight | UIRectCornerTopLeft];
        }
    }else{//不是第一个（没有箭头）
        self.arrows.hidden = YES;
    
        self.contentLabel.sd_layout
        .topSpaceToView(self.contentView, 5 *[UIScreen zj_screenHScale])
        .leftSpaceToView(self.contentView, (62 + 5) * [UIScreen zj_screenWScale])
        .widthIs([UIScreen zj_screenWith] - (62 + 12 + 10) * [UIScreen zj_screenWScale])
        .heightIs([self.contentLabel getLableSizeWithMaxWidth:self.contentLabel.width].height);
        [self.contentLabel updateLayout];
        
        self.bgView.sd_layout
        .topSpaceToView(self.contentView, 0)
        .leftSpaceToView(self.contentView, 62 * [UIScreen zj_screenWScale])
        .rightSpaceToView(self.contentView, 12 * [UIScreen zj_screenWScale])
        .heightIs(self.contentLabel.height + 10 * [UIScreen zj_screenHScale]);
        
        [self.bgView updateLayout];
        
        if (self.talkModel.PARISELIST.count > 0 && (self.talkModel.COMMENTLIST.count == self.indexPath.row)) {//有点赞数组时的最有一个
            self.underLine.hidden = NO;
            [self.bgView setRadius:4 * [UIScreen zj_screenWScale] byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight];
            
        }else  if (! (self.talkModel.PARISELIST.count > 0) && (self.talkModel.COMMENTLIST.count == self.indexPath.row + 1)){//无点赞数组时的最有一个
            
            self.underLine.hidden = NO;
            [self.bgView setRadius:4 * [UIScreen zj_screenWScale] byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight];
            
        }else{//不是最后一个
            self.underLine.hidden = YES;
            [self.bgView setRadius:0 * [UIScreen zj_screenWScale] byRoundingCorners:UIRectCornerAllCorners];
        }
        
    }
}

#pragma mark - Getters

- (UIImageView *)arrows{
    if (_arrows == nil){
        _arrows = [UIImageView new];
        _arrows.image = [UIImage imageNamed:@"chat_bg_sanjiao"];
        _arrows.contentMode = UIViewContentModeBottom;
    }
    return _arrows;
}

- (UILabel *)contentLabel{
    if (_contentLabel == nil){
        _contentLabel = [UILabel new];
        _contentLabel.font = [UIFont subTitleTextFont];
        _contentLabel.textColor = [UIColor textBlackColor];
        _contentLabel.numberOfLines = 0;
        _contentLabel.lineSpace = 4 * [UIScreen zj_screenHScale];
        _contentLabel.characterSpace = 1;
    }
    return _contentLabel;
}

- (UILabel *)underLine{
    if (_underLine == nil){
        _underLine = [UILabel new];
        _underLine.backgroundColor = [UIColor zj_colorWithHexString:@"ececec"];
    }
    return _underLine;
}

- (UIView *)bgView{
    if (_bgView == nil){
        _bgView = [UIView new];
        _bgView.backgroundColor = [UIColor zj_colorWithHexString:@"ececec"];
    }
    return _bgView;
}



@end
